<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
      <el-button tag="a" :href="tenant_admin_url(row)" target="_blank" size="small">打开后台</el-button>
      <el-button tag="a" :href="tenant_home_url(row)" target="_blank" size="small">打开前端</el-button>
      <el-button v-if="/^zsff_/.test(row.name)" @click="zsff_tenant_info(row)" size="small">清除缓存</el-button>
      <el-button v-if="in_right_admin('Tenant/edit') && row.logo && row.logo!=row.head_img" @click="edit_logo(row)" size="small">同步logo</el-button>
    </template>
  </list_url>
</template>
<script>
import {_config, _href, decode_config} from "@/js/s94_tool.js";
import {iframe, in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import router_auto from "@/vue_plugin/router_auto.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('Tenant/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('Tenant/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('Tenant/add')) btns['添加'] = function(){ iframe('tenant/edit') }
      if(in_right_admin('Tenant/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('Tenant/recycle')) btns['回收站'] = function(){ iframe('tenant/index?is_del=1') }
    }
    let forms_config = {
      title: {label:'标题',name:'where[title][like]',type:'text_one',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left'},
      head_img:{name:'head_img', label:'头像', width:'45px', type:'file_img'},
      name:{name:'name', label:'租户名', width:'120px'},
      title:{name:'title', label:'标题', width:'120px'},
      host:{name:'host', label:'自定义域名', min_width:'80px'},
      tenant_package_id:{name:'tenant_package_id', label:'套餐', width:'120px', type:'select_one', config:[]},
      template:{name:'template', label:'模板', width:'180px'},
      expiration_time:{name:'expiration_time', label:'到期时间', width:'165px'},
      allow_expiration:{name:'allow_expiration', label:'允许过期', type: 'duration', width:'165px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'200px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/tenant/'+(is_del?'recycle':'list'),
        title: "租户列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
         s94Mjs.each(list, function (v){
           v.expiration_time = v.expiration_time || '永久';
           v.created = (new Date()).getTime() - (new Date(v.create_time)).getTime();
         })
         return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    in_right_admin: in_right_admin,
    to_edit(row){
      let can_edit = in_right_admin('Tenant/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('tenant/edit?'+s94Mjs.param_encode(param))
    },
    zsff_tenant_info(row) {
      if (!row.host) return this.$message.error('缺少 自定义域名');
      let _url = `http://${row.host}/wap/api/tenant_info`;
      ajax('/home/api/curl?_url='+encodeURIComponent(_url), (res)=>{
        this.$message.success("操作成功");
      })
    },
    tenant_home_url(row){
      if (/^zsff_/.test(row.name) && row.host){
        return `http://${row.host}`;
      }
      let host = row.host ? row.host : window.location.host.replace(/[^\.]+/, row.name);
      return `http://${host}`;
    },
    tenant_admin_url(row) {
      if (/^zsff_/.test(row.name) && row.host){
        return `http://${row.host}/admin/login/index.html`;
      }
      let href = router_auto.resolve('/login').href;
      href = _href(href);
      let host = row.host ? row.host : window.location.host.replace(/[^\.]+/, row.name);
      href = href.replace(/\:\/\/[^\/]+/, '://'+host);
      return href;
    },
    to_init(row){
      iframe('tenant/init?name='+row.name);
    },
    edit_logo(row){
      if (!row.logo) return;
      ajax({url:'/admin/tenant/edit?id='+row.id, data: {head_img: row.logo}}, (res)=>{
        if (res.data > 0) this.$refs['list_url'].list();
        return false;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/tenant_package/list', cache:true}, (res)=>{
      this.config.list_cols.tenant_package_id.config = decode_config(res.data.list, {content_key:'title',value_key:'id'})
      return false;
    })
  }
}
</script>
